package org.lendoo.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.lendoo.web.entity.SellInfo;
import org.lendoo.web.enumeration.ServiceResultEnum;
import org.lendoo.web.pojo.common.ServiceResult;
import org.lendoo.web.pojo.dto.HotGoodsDto;
import org.lendoo.web.pojo.dto.RelGoodsImageUrlDto;
import org.springframework.stereotype.Service;
import org.lendoo.web.mapper.GoodsMapper;
import org.lendoo.web.entity.Goods;
import org.lendoo.web.service.GoodsService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * GoodsService接口的实现类
 */
@Slf4j
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Resource
    private ServiceHelper serviceHelper;

    /**
     * 新增
     * @param goods 商品
     * @return 添加成功返回1，添加失败返回-1
     */
    @Override
    public ServiceResult<Boolean> add(Goods goods) {
        baseMapper.insert(goods);
        return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 批量导入
     * @param goodsList 商品（列表）
     * @return 批量导入成功返回1，批量导入失败返回-1
     */
    @Override
    public ServiceResult<Boolean> batchAdd(List<Goods> goodsList) {
        baseMapper.insertBatchSomeColumn(goodsList);
        return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 根据id更新
     * @param goods 商品
     * @return 更新成功返回1，更新失败返回-1
     */
    @Override
    public ServiceResult<Boolean> updateForId(Goods goods) {
        baseMapper.updateById(goods);
        return new ServiceResult<>(ServiceResultEnum.UPDATE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.UPDATE_SUCCESS.getMessage());
    }

    /**
     * 根据id删除
     * @param id 主键
     * @return 删除成功返回1，删除失败返回-1
     */
    @Override
    public ServiceResult<Boolean> deleteById(Serializable id) {
        baseMapper.deleteById(id);
        return new ServiceResult<>(ServiceResultEnum.DELETE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.DELETE_SUCCESS.getMessage());
    }

    /**
     * 根据商品id，查询商品和对应的图片列表
     * @param goodsId 主键
     * @return 返回查询结果
     */
    @Override
    public ServiceResult<RelGoodsImageUrlDto> findGoodsAndImageUrlListByGoodsId(Integer goodsId) {
        Goods goods = serviceHelper.getGoodsMapper().selectById(goodsId);
        if (null != goods) {
            List<String> imageUrlList = serviceHelper.getGoodsMapper().findImageUrlListByGoodsId(goodsId);
            RelGoodsImageUrlDto relGoodsImageUrlDto = new RelGoodsImageUrlDto();
            relGoodsImageUrlDto.setGoods(goods);
            relGoodsImageUrlDto.setImageUrlList(imageUrlList);
            return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), relGoodsImageUrlDto, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
        } else {
            return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), null, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
        }
    }

    /**
     * 根据条件，分页查询，升序/降序排列
     * @param goods 商品
     * @param pageNo 页号
     * @param pageSize 每页行数
     * @return 返回查询结果列表
     */
    @Override
    public ServiceResult<PageInfo<List<RelGoodsImageUrlDto>>> page(Goods goods, int pageNo, int pageSize) {
        List<RelGoodsImageUrlDto> relGoodsImageUrlDtoList = new ArrayList<>();

        PageHelper.startPage(pageNo, pageSize);
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Goods::getCategoryId, goods.getCategoryId());
        List<Goods> goodsList = baseMapper.selectList(lambdaQueryWrapper);

        if (null != goodsList && goodsList.size() > 0){
            for (Goods theGoods : goodsList){
                List<String> urlList = serviceHelper.getRelGoodsImageMapper().findGoodsImageUrlListByGoodsId(theGoods.getId());
                RelGoodsImageUrlDto relGoodsImageUrlDto = new RelGoodsImageUrlDto();
                relGoodsImageUrlDto.setImageUrlList(urlList);
                relGoodsImageUrlDto.setGoods(theGoods);
                relGoodsImageUrlDtoList.add(relGoodsImageUrlDto);

                LambdaQueryWrapper<SellInfo> sellInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                sellInfoLambdaQueryWrapper.eq(SellInfo::getGoodsId, theGoods.getId());
                Integer sellCount = serviceHelper.getSellInfoMapper().selectCount(sellInfoLambdaQueryWrapper);
                relGoodsImageUrlDto.setSellCount(sellCount);
            }
        }
        ServiceResult<PageInfo<List<RelGoodsImageUrlDto>>> serviceResult = new ServiceResult<>();
        PageInfo pageInfo = new PageInfo<>(relGoodsImageUrlDtoList);
        serviceResult.setResult(pageInfo);
        serviceResult.setMessage(ServiceResultEnum.SELECT_SUCCESS.getMessage());
        serviceResult.setCode(ServiceResultEnum.SELECT_SUCCESS.getCode());
        serviceResult.setSuccess(Boolean.TRUE);
        return serviceResult;
//        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), new PageInfo<>(relGoodsImageUrlDtoList), Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 查询所有记录
     * @return
     */
    @Override
    public ServiceResult<List<Goods>> findAll() {
        List<Goods> goodsList = baseMapper.selectList(null);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), goodsList, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 查找所有热门商品
     * @return
     */
    @Override
    public ServiceResult<List<HotGoodsDto>> findHotGoods() {
        List<HotGoodsDto> goodsList = serviceHelper.getGoodsMapper().findHotGoods();
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), goodsList, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }
}
